DAY33:What's a Perfect Power anyway?


Posted by birdbirdmurmur on 2023-08-14

題目連結

https://www.codewars.com/kata/54d4c8b08776e4ad92000835

解法

function isPP(n) {
    for (let i = 2; i <= n; i++) {
        let m = Math.round(Math.pow(n, 1 / i));

        if (m ** i === n) {
            return [m, i];
        }
    }
    return null;
}

筆記

先查了一下MDN看要怎麼開幾次根號
最後用了Math.pow(n, 1 / i)
開根號倒過來就是次方
使用Math.round四捨五入
反過來乘回去 就可以拿出來判斷了


#javascript #Codewars #Math.round #Math.pow







Related Posts

HTML 基礎

HTML 基礎

[6] 進階資料型別 part1 - 字串、List 操作

[6] 進階資料型別 part1 - 字串、List 操作

Day 39 & 40 - Flight Deal Finder [BIG project]

Day 39 & 40 - Flight Deal Finder [BIG project]


Comments